package com.exhibition.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.exhibition.service.ITbUnitService;
import com.exhibition.util.Util;

@Controller
public class TbUnitController {
	
	@Autowired
	ITbUnitService tbUnitService;
	
	@RequestMapping("admin/unitInfo.do")	
	public ModelAndView unitInfo(HttpServletRequest request){
		ModelAndView mav=new ModelAndView("./unit/update.jsp");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("unit_id", request.getSession().getAttribute("account"));
		mav.addObject("unit", tbUnitService.getOne(map));
		return mav;
	}
	
	@RequestMapping("admin/unitList.do")	
	public ModelAndView list(HttpServletRequest request,String unit_name){
		ModelAndView mav=new ModelAndView("./unit/list.jsp");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("unit_name", unit_name);
		mav.addObject("list", tbUnitService.getList(map));
		mav.addObject("unit_name", unit_name);
		return mav;
	}
	@RequestMapping("admin/unitAddOk.do")	
	public ModelAndView addOk(HttpServletRequest request,
			String unit_account,
			String unit_pwd,
			String unit_name,
			String unit_address,
			String unit_size,
			String unit_industry,
			String unit_nature){
		ModelAndView mav=new ModelAndView("redirect:/admin/unitList.do");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("unit_id", Util.getPrimaryKey());
		map.put("unit_account", unit_account);
		map.put("unit_pwd", unit_pwd);
		map.put("unit_name", unit_name);
		map.put("unit_address", unit_address);
		map.put("unit_size", unit_size);
		map.put("unit_industry", unit_industry);
		map.put("unit_nature", unit_nature);
		tbUnitService.add(map);
		return mav;
	}
	@RequestMapping("unit/registerOk.do")	
	public ModelAndView register(HttpServletRequest request,
			String unit_account,
			String unit_pwd,
			String unit_name,
			String unit_address,
			String unit_size,
			String unit_industry,
			String unit_nature){
		ModelAndView mav=new ModelAndView("../login.jsp");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("unit_id", Util.getPrimaryKey());
		map.put("unit_account", unit_account);
		map.put("unit_pwd", unit_pwd);
		map.put("unit_name", unit_name);
		map.put("unit_address", unit_address);
		map.put("unit_size", unit_size);
		map.put("unit_industry", unit_industry);
		map.put("unit_nature", unit_nature);
		tbUnitService.add(map);
		return mav;
	}
	@RequestMapping("admin/unitUpdate.do")	
	public ModelAndView update(HttpServletRequest request,String unit_id){
		ModelAndView mav=new ModelAndView("./unit/update.jsp");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("unit_id", unit_id);
		mav.addObject("unit", tbUnitService.getOne(map));
		return mav;
	}
	@RequestMapping("admin/unitUpdateOk.do")	
	public ModelAndView updateOk(HttpServletRequest request,
			String unit_id,
			String unit_account,
			String unit_pwd,
			String unit_name,
			String unit_address,
			String unit_size,
			String unit_industry,
			String unit_nature
			){
		ModelAndView mav=new ModelAndView("redirect:/admin/unitList.do");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("unit_id", unit_id);
		map.put("unit_account", unit_account);
		map.put("unit_pwd", unit_pwd);
		map.put("unit_name", unit_name);
		map.put("unit_address", unit_address);
		map.put("unit_size", unit_size);
		map.put("unit_industry", unit_industry);
		map.put("unit_nature", unit_nature);
		tbUnitService.update(map);
		return mav;
	}
	@RequestMapping("admin/unitDelete.do")	
	public ModelAndView delete(HttpServletRequest request,String unit_id){
		ModelAndView mav=new ModelAndView("redirect:/admin/unitList.do");
		tbUnitService.delete(unit_id);
		return mav;
	}
	@RequestMapping("unit/unitInfo.do")	
	public ModelAndView unitInfo1(HttpServletRequest request){
		ModelAndView mav=new ModelAndView("./myinfo/info.jsp");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("unit_id", request.getSession().getAttribute("unit_id"));
		mav.addObject("unit", tbUnitService.getOne(map));
		return mav;
	}
	@RequestMapping("unit/unitInfoOk.do")	
	public ModelAndView unitInfoOk(HttpServletRequest request,
			  String unit_id,
			  String unit_name,
			  String unit_pwd,
			  String unit_address,
			  String unit_size,
			  String unit_industry,
			  String unit_nature
			){
		ModelAndView mav=new ModelAndView("redirect:/unit/unitInfo.do");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("unit_id", unit_id);
		map.put("unit_name", unit_name);
		map.put("unit_pwd", unit_pwd);
		map.put("unit_address", unit_address);
		map.put("unit_size", unit_size);
		map.put("unit_industry", unit_industry);
		map.put("unit_nature", unit_nature);
		tbUnitService.update(map);
		return mav;
	}
}
